//
//  MVPUserViewController.m
//  CharlesMVX
//
//  Created by Charles on 2020/3/11.
//  Copyright © 2020 Charles. All rights reserved.
//

#import "MVPUserViewController.h"
#import "BlogViewController.h"
#import "MVCUserInfoController.h"
#import "DetailViewController.h"
#import "BlogViewPresenter.h"
#import "UserInfoView.h"
#import "Masonry.h"
#import "MVCUserViewController+Protect.h"
#import "DraftViewController.h"
#import "UIView+Extension.h"


@interface MVPUserViewController ()<BlogViewControllerCallBack>

@property (assign, nonatomic) NSUInteger userId;

@property (strong, nonatomic) UIButton *blogButton;
@property (strong, nonatomic) UIButton *draftButton;
@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) BlogViewController *blogVC;
@property (strong, nonatomic) DraftViewController *draftVC;
@property (strong, nonatomic) MVCUserInfoController *userInfoVC;
@end

@implementation MVPUserViewController

+ (instancetype)instanceWithUserId:(NSUInteger)userId {
    return [[MVPUserViewController alloc] initWiWithUserId:userId];
}

- (instancetype)initWiWithUserId:(NSUInteger)userId {
    if (self = [super init]) {
        self.userId = userId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configuation];
    [self addUI];
    [self fetchData];
}


- (void)configuation {
    self.title = [NSString stringWithFormat:@"用户%ld", self.userId];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //因为MVP中的PV关系理论上是多对多的, 所以P层通常都是由MVP的上层直接分配而不是在C层默认创建, 这样在逻辑变动布局不变的情况下, 上层只需要分配一个新的P层就行了, 同理, 如果逻辑不变布局变了, 上层就替换V层即可.
    //更标准的写法是针对每个M都有对应V,C,P, V负责布局 ,C负责和上层交互(某个业务场景的具体需求, 比如HUD), P负责业务逻辑(各种格式化, 各种命令), 但这里因为我偷懒没有建立DraftView而是将TableViewDataSource布局直接写在C层了, 所以此时的DraftViewController即是C也是V.
    
    self.userInfoVC = [MVCUserInfoController instanceWithUserId:self.userId];
    [self.userInfoVC setVCGenerator:^UIViewController *(id params) {
        return [[DetailViewController alloc]init];
    }];
    
    self.blogVC = [BlogViewController instanceWithPresenter:[BlogViewPresenter presenterWithUserId:self.userId]];
    self.blogVC.delegate = self;//BlogViewController走的是Protocol绑定方式
    
    __weak typeof(self) weakSelf = self;
    self.draftVC = [DraftViewController instanceWithPresenter:[DraftViewPresenter presenterWithUserId:self.userId]];
    [self.draftVC setDidSelectedRowHandler:^(Draft * draft) {
         [weakSelf.navigationController pushViewController:[[DetailViewController alloc]init] animated:YES];
    }];
}

- (void)addUI {
    [self.view addSubview:self.userInfoVC.userInfoView];
    [self.userInfoVC.userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.inset(60);
        make.left.right.inset(0);
        make.height.mas_equalTo(260);
    }];
    
    UIButton *(^makeButton)(NSString *) = ^(NSString *title) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(switchTableView:) forControlEvents:UIControlEventTouchUpInside];
        return button;
    };
    
    [self.view addSubview:self.blogButton = makeButton(@"博客")];
    [self.view addSubview:self.draftButton = makeButton(@"草稿")];
    
    [self.blogButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(20);
        make.top.equalTo(self.userInfoVC.userInfoView.mas_bottom);
        make.right.equalTo(self.draftButton.mas_left);
        make.width.equalTo(self.draftButton.mas_width);
        make.height.mas_equalTo(40);
    }];
    
    [self.draftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(20);
        make.top.equalTo(self.userInfoVC.userInfoView.mas_bottom);
        make.left.equalTo(self.draftButton.mas_right);
        make.width.equalTo(self.draftButton.mas_width);
        make.height.mas_equalTo(40);
    }];
    
    self.blogButton.selected = YES;

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    scrollView.scrollEnabled = NO;
    scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.scrollView = scrollView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.top.equalTo(self.blogButton.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    [self.scrollView addSubview:self.blogVC.tableView];
    [self.scrollView addSubview:self.draftVC.tableView];
    
    
    [self.blogVC.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.inset(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(self.scrollView.mas_height);
    }];
    
    [self.draftVC.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.inset(0);
        make.left.equalTo(self.blogVC.tableView.mas_right);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(self.scrollView.mas_height);
    }];
}

- (void)fetchData {
    [self.userInfoVC fetchData];
    [self showHUD];
    [self.blogVC.presenter fetchDataWithCompletionHandler:^(NSError *error, id result) {
        [self hideHUD];
    }];
    
    [self.draftVC fetchDataWithCompletionHandler:nil];
}

- (void)blogViewControllerdidSelectBlog:(Blog *)blog {
    [self.navigationController pushViewController:[[DetailViewController alloc]init] animated:YES];
}

- (void)switchTableView:(UIButton *)sender {
    if (sender == self.blogButton) {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
        self.draftButton.selected = NO;
    } else {
        
        self.blogButton.selected = NO;
        [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    }
    sender.selected = YES;
}

@end
